package com.jiafa.wjxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiafa.wjxt.entity.TUserProject;
import com.jiafa.wjxt.entity.TUserProjectItem;
import com.jiafa.wjxt.mapper.TUserProjectItemMapper;
import com.jiafa.wjxt.mapper.TUserProjectMapper;
import com.jiafa.wjxt.service.TUserProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiafa.wjxt.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiafa
 * @since 2021-05-31
 */
@Service
public class TUserProjectServiceImpl extends ServiceImpl<TUserProjectMapper, TUserProject> implements TUserProjectService {

    @Autowired
    TUserProjectMapper tUserProjectMapper;
    @Autowired
    TUserProjectService tUserProjectService;
    @Autowired
    TUserProjectItemMapper tUserProjectItemMapper;

    @Override
    public Result myProjects(Long current, Long limit, Long userId, Boolean isDeleted,String title) {
        Result result = Result.build();
        Page<TUserProject> tUserProjectPage = new Page<>(current,limit);
        QueryWrapper<TUserProject> wrapper = new QueryWrapper<>();
        if (userId != null){
            wrapper.eq("user_id",userId);
        }
        if (isDeleted != null){
            wrapper.eq("is_deleted",isDeleted);
        }
        if(title != null && !title.equals("")){
            wrapper.like("title",title);
        }
        wrapper.orderByDesc("update_time");

        tUserProjectMapper.selectPage(tUserProjectPage,wrapper);

        result.setCode(200);
        result.setData(tUserProjectPage);
        return result;
    }

    @Override
    public Result RecycleProjects(Long userId, Boolean isDeleted) {
        Result result = Result.build();
        QueryWrapper<TUserProject> wrapper = new QueryWrapper<>();
        Page<TUserProject> page = new Page<>();
        if(userId != null){
            wrapper.eq("user_id",userId);
        }
        if(isDeleted != null){
            wrapper.eq("is_deleted",isDeleted);
        }
        wrapper.orderByDesc("update_time");
        tUserProjectMapper.selectPage(page,wrapper);
        result.setCode(200);
        result.setData(page);
        return result;
    }

    @Override
    public Result queryByTitle(String title) {
        Result result = Result.build();
        QueryWrapper<TUserProject> wrapper = new QueryWrapper<>();
        wrapper.like("title",title);
        List<TUserProject> tUserProjectList = tUserProjectMapper.selectList(wrapper);
        result.setCode(200);
        result.setData(tUserProjectList);
        return result;
    }

    @Override
    public TUserProject queryByKey(String projectKey) {
        Result result = Result.build();
        QueryWrapper<TUserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("project_key",projectKey);
        TUserProject tUserProject = tUserProjectMapper.selectOne(wrapper);
        return tUserProject;
    }

    @Override
    public Result queryProjectItem(String projectKey) {
        Result result = Result.build();
        QueryWrapper<TUserProjectItem> wrapper = new QueryWrapper<>();
        System.out.println(projectKey);
        if(projectKey != null){
            wrapper.eq("project_key",projectKey);
        }
        List<TUserProjectItem> tUserProjectItems = tUserProjectItemMapper.selectList(wrapper);
        result.setCode(200);
        result.setData(tUserProjectItems);
        return result;
    }

    @Override
    public Result changeStats(String projectKey){
        Result result = Result.build();
        QueryWrapper<TUserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("project_key",projectKey);
        TUserProject tUserProject = tUserProjectMapper.selectOne(wrapper);
        tUserProject.setStats(2);
        tUserProjectService.update(tUserProject,wrapper);
        return Result.ok(""+tUserProject.getStats());
    }

    @Override
    public Result toStop(String projectKey){
        Result result = Result.build();
        QueryWrapper<TUserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("project_key",projectKey);
        TUserProject tUserProject = tUserProjectMapper.selectOne(wrapper);
        tUserProject.setStats(3);
        tUserProjectService.update(tUserProject,wrapper);
        return Result.ok(""+tUserProject.getStats());
    }

    @Override
    public Result toDelete(String projectKey){
        Result result = Result.build();
        QueryWrapper<TUserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("project_key",projectKey);
        TUserProject tUserProject = tUserProjectMapper.selectOne(wrapper);
        tUserProject.setIsDeleted(true);
        tUserProjectService.update(tUserProject,wrapper);
        return Result.ok(""+tUserProject.getIsDeleted());
    }

    @Override
    public Result recovery(String projectKey){
        Result result = Result.build();
        QueryWrapper<TUserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("project_key",projectKey);
        TUserProject tUserProject = tUserProjectMapper.selectOne(wrapper);
        tUserProject.setIsDeleted(false);
        tUserProjectService.update(tUserProject,wrapper);
        return Result.ok(""+tUserProject.getIsDeleted());
    }
}
